#include "CFrame.h"
#include "CRoom.h"
#include <vector>

using namespace std;

CFrame::CFrame(string title, int width, int height) {
	VGCVirtualGameConsole::initialize(title, width, height);
}

CFrame::~CFrame() {
	destroy();
}

void CFrame::addRoom(CRoom &room) {
	this->rooms.push_back(&room);
}

void CFrame::run() {
	while (VGCVirtualGameConsole::beginLoop()) {
		if (VGCDisplay::beginFrame()) {
			step(VGCClock::getTime());
			draw(VGCClock::getTime());

			VGCDisplay::endFrame();
		}
		VGCVirtualGameConsole::endLoop();
	}
	destroy();
}

void CFrame::step(const double &time) {
	for (vector<CRoom*>::iterator i = rooms.begin(); i != rooms.end(); i++) {
		(*i)->step(time);
	}
}

void CFrame::draw(const double &time) {
	for (vector<CRoom*>::iterator i = rooms.begin(); i != rooms.end(); i++) {
		(*i)->draw(time);
	}
}

void CFrame::destroy() {
	for (vector<CRoom*>::iterator i = rooms.begin(); i != rooms.end(); i++) {
		(*i)->destroy();
		delete (*i);
	}

	VGCVirtualGameConsole::finalize();
}